﻿
<!--#include file="includes/database.asp" -->
<!--#include file="includes/global_varaiable.asp" -->
<!--#include file="theloai_info.asp" -->

<%
Response.expires = 0
Response.expiresabsolute = Now() - 1
Response.addHeader "pragma", "no-cache"
Response.addHeader "cache-control", "private"
Response.addHeader "cache-control", "no-cache"
Response.addHeader "cache-control", "no-store"
Response.CacheControl = "no-cache"

Response.Buffer = True

Const ewSessionMessage = "Message"

' Load key from QueryString
x_GenreID = Request.QueryString("GenreID")

' Get action
sAction = Request.Form("a_edit")
If sAction = "" Or IsNull(sAction) Then
	sAction = "I"	' Display with input box
Else

	' Get fields from form
	x_GenreID = Request.Form("x_GenreID")
	x_Genre = Request.Form("x_Genre")
	
End If

' Check if valid key
'If x_GenreID = "" Or IsNull(x_GenreID) Then Response.Redirect "theloai_list.asp"

Select Case sAction
	Case "I": ' Get a record to display
		If Not LoadData() Then ' Load Record based on key
			Session(ewSessionMessage) = "Không có dữ liệu"
			adoCon.Close ' Close adoConection
			Set adoCon = Nothing
			Response.Clear
			Response.Redirect "theloai_list.asp"
		End If
	Case "U": ' Update
		If EditData() Then ' Update Record based on key
			Session(ewSessionMessage) = "Cập nhật thành công"
			adoCon.Close ' Close adoConection
			Set adoCon = Nothing
			Response.Clear
			Response.Redirect "theloai_list.asp"
		End If
End Select
%>
<!--#include file="header.asp"-->

<script type="text/javascript" src="ew.js"></script>

<script type="text/javascript">
<!--
if (EW_this.x_Genre && !EW_hasValue(EW_this.x_Genre, "TEXT" )) {
	if (!EW_onError(EW_this, EW_this.x_Genre, "TEXT", "Vui lòng nhập dữ liệu - Genre"))
		return false; 
}
return true;
}
//-->
</script>

<p><span class="aspmaker">Edit TABLE: THELOAI<br><br><a href="theloai_list.asp">Back to List</a></span></p>
<form name="theloai_edit" id="theloai_edit" action="theloai_edit.asp" method="post" onSubmit="return EW_checkMyForm(this);">
<p>
<input type="hidden" name="a_edit" value="U">
<table class="ewTable">
	<!-- <input type="hidden" id="x_GenreID" name="x_GenreID" value="<%= x_GenreID %>"> -->
	
	<tr id="r_GenreID">
		<td class="ewTableHeader"><span>Genre ID</span></td>
		<td class="ewTableAltRow"><span id="cb_x_GenreID">
		<input type="text" name="x_GenreID" id="x_GenreID" size="30" disabled value="<%= x_GenreID %>">
		</span></td>
	</tr>
	
	<tr id="r_Genre">
		<td class="ewTableHeader"><span>Genre</span></td>
		<td class="ewTableAltRow"><span id="cb_x_Genre">
		<input type="text" name="x_Genre" id="x_Genre" size="30" value="<%= Session("Genre") %>">
		</span></td>
	</tr>
</table>
<p>
<input type="submit" name="btnAction" id="btnAction" value="EDIT">
</form>
<!--#include file="footer.asp"-->
<%
adoCon.Close ' Close adoConection
Set adoCon = Nothing
%>
<%

'-------------------------------------------------------------------------------
' Function LoadData
' - Load Data based on Key Value
' - Variables setup: field variables

Function LoadData()
	
	Dim rs, sSql, sFilter
	sFilter = ewSqlKeyWhere
	sFilter = Replace(sFilter, "@GenreID", AdjustSql(x_GenreID)) ' Replace key value
	sSql = ewBuildSql(ewSqlSelect, ewSqlWhere, ewSqlGroupBy, ewSqlHaving, ewSqlOrderBy, sFilter, "")
	
	Set rs = Server.CreateObject("ADODB.Recordset")
	
	rs.Open sSql, adoCon
	If rs.Eof Then
		LoadData = False
	Else
		LoadData = True
		rs.MoveFirst

		' Get the field contents
		x_GenreID = rs("GenreID")
		x_Genre = rs("Genre")
		
		Session("GenreID") = x_GenreID
		Session("Genre") = x_Genre
		
	End If
	rs.Close
	Set rs = Nothing
End Function
%>
<%

'-------------------------------------------------------------------------------
' Function EditData
' - Edit Data based on Key Value
' - Variables used: field variables

Function EditData()
	Dim rs, sSql, sFilter
	sFilter = ewSqlKeyWhere
	sFilter = Replace(sFilter, "@GenreID", AdjustSql(Session("GenreID"))) ' Replace key value
	sSql = ewBuildSql(ewSqlSelect, ewSqlWhere, ewSqlGroupBy, ewSqlHaving, ewSqlOrderBy, sFilter, "")
	Set rs = Server.CreateObject("ADODB.Recordset")
	rs.CursorLocation = 3
	
	'response.write(Session("GenreID"))
	
	rs.Open sSql, adoCon, 1, 2
	
	' clone old and new rs object
	Dim rsold, rsnew
	Set rsold = rs.Clone(1)
	rsold.Requery()
	Set rsnew = rs.Clone(1)
	If rs.Eof Then
		EditData = False ' Update Failed
	Else

		' Field Genre
		sTmp = Trim(x_Genre)
		If Trim(sTmp) = "" Then sTmp = Null
		rs("Genre") = sTmp
		
		response.write(sTmp)

		' Call updating event
		If Recordset_Updating(rs, rsnew) Then
			rs.Update
			EditData = True
		Else
			rs.CancelUpdate
			EditData = False
		End If
	End If

	' Call updated event
	If EditData Then
		Call Recordset_Updated(rsold, rsnew)
	End If
	rs.Close
	Set rs = Nothing
	rsold.Close
	Set rsold = Nothing
	rsnew.Close
	Set rsnew = Nothing
End Function

'-------------------------------------------------------------------------------
' Recordset updating event

Function Recordset_Updating(rsold, rsnew)
	On Error Resume Next

	' Please enter your customized codes here
	Recordset_Updating = True
End Function

'-------------------------------------------------------------------------------
' Recordset updated event

Sub Recordset_Updated(rsold, rsnew)
	On Error Resume Next
End Sub
%>
